/**
 *
 */
package com.test.dao.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.test.dao.UserDao;
import com.test.domain.User;
import com.test.util.JdbcUtil;

/**
 * @author Administrator
 * @date 2018年5月27日 下午2:47:09
 * 
 */
public class UserDaoImpl2 implements UserDao {

	@Override
	public void add(User user) {
		String sql = "INSERT INTO user(name,password,age) values('"+ user.getName() +
				"','" + user.getPassword() + "',"+ user.getAge() + ")";
		JdbcUtil.excute(sql);
	}

	@Override
	public void mod(User user) {

	}

	@Override
	public void del(int id) {

	}

	@Override
	public List<User> query(User user) {
		String sql = "select * from user where name like ? ";
		
//		if(user.getName() != null || user.getName() !="") {
//			sql += "name like '" + user.getName() +"%'";
//		}
		
		
		System.out.println(sql);
		
		
		List<Map> lists = JdbcUtil.query(sql,user.getName()+"%");
		List<User> result = new ArrayList<>();
		for(Map map : lists) {
			User tempUser = new User();
			try {
				BeanUtils.populate(tempUser, map);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			result.add(tempUser);
		}
		return result;
	}

	@Override
	public int queryByNameAndPassword() {
		return 0;
	}

}
